#include <stdio.h>
#include <stdlib.h>
#include "aimage.h"
#include "aimage_utils.h"
#include "aimage_dcraw.h"
#include "aimage_ppm.h"

int main( int argc, char *argv[] )
{
	AIMAGE image;
	AIMAGE dark;
	AIMAGE hot;

	if ( argc != 1 && argc != 2 )
	{
		fprintf(stderr, "%s [in_file]\n", argv[0] );
		exit( 1 );
	}

	if ( argc == 2 )
	{
		AIMAGE_DCRAW_UTILS::load( image, argv[ 1 ] );
	}
	else
	{
		AIMAGE_DCRAW_UTILS::load( image, "stdin" );
	}

	int width, height;

	image.get_dim( width, height );

	FILE *f;
	f = fopen("dark.ppm", "rb");
	if ( f != NULL)
	{
		fclose( f );
		AIMAGE_PPM_UTILS::load( dark,"dark.ppm");
		fprintf(stderr,"Using dark frame\n");
	}
	else
	{
		dark.alloc_image( width, height );
		AIU::cls( dark, 0, 0, 0 );
	}
	f = fopen("hotmap.ppm", "rb");
	if ( f!= NULL )
	{
		fclose( f );
		AIMAGE_PPM_UTILS::load( hot,"hotmap.ppm");
		fprintf(stderr,"Using Hot Map\n");
	}
	else
	{
		hot.alloc_image( width, height );
		AIU::cls( hot, 0, 0, 0 );
	}

	AIU::correct_for_hotmap( image, hot);
	AIU::subtract( image, dark ); 

	AIMAGE_PPM_UTILS::save( image, "stdout", true );
}

